<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	/* 解决table中纯英文不换行 */
	#dynamic-table td {word-break: break-all;white-space: pre-wrap;max-width: 27%;}
	#dynamic-table td:nth-child(2) {width: 27%;}
	#dynamic-table td:nth-child(4) {width: 27%;}
</style>
<div class="page-header">
	<h1>
		食谱列表
		<a href="<?=admin_url('data/save_recipe')?>" class="btn btn-info a-custom">
			<i class="ace-icon fa fa-plus"></i> 发布食谱
		</a>
	</h1>
</div>
<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>食谱名称</th>
				<th>发布班级</th>
                <th>菜品分类</th>
				<th>菜品时段</th>
				<th>菜品管理</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){
		// 食谱表格
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('data/get_recipe_list')?>',
			"columns": [
	            {"data": "title"},
	            {"data": function(ret){
	            	var html = "";
	            	for(var i in ret.classList) html += '<span class="label label-success mr10">'+ ret.classList[i]['class_name'] +'</span>';
	            	return html;
	            }},
	            {"data": function(ret){
	            	var html = "";
	            	for(var i in ret.cateTitleArr) html += '<span class="label label-primary mr10">'+ ret.cateTitleArr[i] +'</span>';
	            	return html;
	            }},
	            {"data": function(ret){
	            	var html = "";
	            	for(var i in ret.timeArr) html += '<span class="label label-success mr10">'+ ret.timeArr[i] +'</span>';
	            	return html;
	            }},
	            {"data": function(ret){
	            	return '<a class="green" href="<?=admin_url('data/recipe_detail')?>/'+ ret.id +'"><i class="fa fa-gears"></i> 查看</a>';
	            }},
	            {"data": function(ret){
					var action = '';
					action += '<a class="green" href="<?=admin_url('data/save_recipe')?>/'+ ret.id +'"><i class="fa fa-edit"></i> 编辑</a>';
					action += '&nbsp;&nbsp;&nbsp;&nbsp;<a class="text-danger delete-recipe" href="javascript:" data-id=' + ret.id +  ' data-title=' + ret.title + '><i class="fa fa-times"></i> 删除</a>';
					return action;
	            }},
			]
		});
	
		// 删除班级食谱
		$("body").on('click', '.delete-recipe', function() {
			var id = $(this).data('id');
			var title = $(this).data('title');
			var posturl = "<?= admin_url("data/delete_recipe_action") ?>";
			var data = {
				'id': id
			};
			layer.confirm('确定删除：' + title + '？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.href = "<?= admin_url('data/recipe') ?>";
				});
			});
		});
	})
</script>